using UnityEngine;
using System.Collections;

public class AndroidInputOptions : MonoBehaviour {

	private GameObject lastGameObject;
	private bool ready;
	private bool inLast = false;
	private GameObject actualGameObject;
	private bool receivedOffset;
		
	
	void Update () 
	{
		
		if (Input.touchCount == 1 && Input.touches[0].phase == TouchPhase.Ended)
        {

            Ray r = Camera.mainCamera.ScreenPointToRay(new Vector3(Input.touches[0].position.x, Input.touches[0].position.y, 0f));
            RaycastHit hit;

            if (Physics.Raycast(r, out hit, 50))
			{
				gameObject.SendMessage("OnTouchDown",hit.transform.gameObject,SendMessageOptions.DontRequireReceiver);
			}
		}
		
		
		if (Input.touchCount == 1)
		{
            Ray r = Camera.mainCamera.ScreenPointToRay(new Vector3(Input.touches[0].position.x, Input.touches[0].position.y, 0f));
            RaycastHit hit;

            if (Physics.Raycast(r, out hit, 50))
			{
				if(!actualGameObject)
				{
					actualGameObject = hit.transform.gameObject;
					gameObject.SendMessage("OnTouchEnter",actualGameObject,SendMessageOptions.DontRequireReceiver);
				}
				
				if(actualGameObject.name != hit.transform.name)
				{
					gameObject.SendMessage("OnTouchExit",actualGameObject,SendMessageOptions.DontRequireReceiver);
					actualGameObject = hit.transform.gameObject;
					gameObject.SendMessage("OnTouchEnter",actualGameObject,SendMessageOptions.DontRequireReceiver);

				}
				else
				{
					gameObject.SendMessage("OnTouchEnter",actualGameObject,SendMessageOptions.DontRequireReceiver);
				}
			}
			
			else
			{
				gameObject.SendMessage("OnTouchExit",actualGameObject,SendMessageOptions.DontRequireReceiver);
			}
		
			

		}
		
		else
		{
			gameObject.SendMessage("OnTouchExit",actualGameObject,SendMessageOptions.DontRequireReceiver);
		}
		
	}
	
	public void SetTargetObject(GameObject go)
	{
		actualGameObject = go;
	}
	
	public void setReceivedOffset(bool state)
	{
		receivedOffset = state;	
	}


}
